bitkeeper revision 1.1159.1.292 (41800989G4HjOPTJHRAf8UYug6chUA)
authorcl349@freefall.cl.cam.ac.uk <cl349@freefall.cl.cam.ac.uk>
Wed, 27 Oct 2004 20:48:09 +0000 (20:48 +0000)
committercl349@freefall.cl.cam.ac.uk <cl349@freefall.cl.cam.ac.uk>
Wed, 27 Oct 2004 20:48:09 +0000 (20:48 +0000)
Parse bare words as config files making the -f before a config filename
optional.
Make parsing intermingled ``getopt'' and ``var=val'' options sane and
add support for a default option handler for bare words.

tools/python/xen/xm/create.py
tools/python/xen/xm/opts.py

index fe2a2e4f0429c93e15c65e43735980f30b8a9504..1c0fa6cb4f8b8f26e610f4ddc1d799773cd1b33d 100644 (file)
@@ -55,6 +55,8 @@ gopts.opt('defconfig', short='f', val='FILE',
           After the script is loaded, option values that were not set on the
           command line are replaced by the values set in the script.""")
 
+gopts.default('defconfig')
+
 gopts.opt('config', short='F', val='FILE',
           fn=set_value, default=None,
           use="""Domain configuration to use (SXP).
index 5e7d69a39876288c8e305ba0671ccbf182dcfb78..0dfee0b7c9ae8af9ebff22d35f238f137d139d7b 100644 (file)
@@ -204,6 +204,8 @@ class Opts:
         self.vals.quiet = 0
         # Variables for default scripts.
         self.vars = {}
+        # Option to use for bare words.
+        self.default_opt = None
 
     def __repr__(self):
         return '\n'.join(map(str, self.options))
@@ -221,6 +223,15 @@ class Opts:
         self.options_map[name] = x
         return x
 
+    def default(self, name):
+        self.default_opt = name
+
+    def getdefault(self, val):
+        if self.default_opt is None:
+            return 0
+        opt = self.option(self.default_opt)
+        return opt.set(val)
+
     def var(self, name, **args):
         x = OptVar(self, name, **args)
         self.options.append(x)
@@ -284,27 +295,27 @@ class Opts:
         """
         self.argv = argv
 
-        try:
-            (vals, args) = getopt(argv[1:], self.short_opts(), self.long_opts())
-        except GetoptError, err:
-            self.err(str(err))
-
-       # hack to work around lack of gnu getopts parsing in python 2.2
-       xargs = args
-       while xargs[1:]:
-           (v,xargs) = getopt(xargs[1:], self.short_opts(), self.long_opts())
-           vals = vals + v
-
-       # back to the real work
-        self.args = args
-        for (k, v) in vals:
-            for opt in self.options:
-                if opt.specify(k, v): break
-            else:
-                print >>sys.stderr, "Error: Unknown option:", k
-                self.usage()
+        # hack to work around lack of gnu getopts parsing in python 2.2
+        args = argv[1:]
         xargs = []
-        for arg in args:
+        while args:
+            # let getopt parse whatever it feels like -- if anything
+            try:
+                (xvals, args) = getopt(args[0:],
+                                       self.short_opts(), self.long_opts())
+            except GetoptError, err:
+                self.err(str(err))
+                
+            for (k, v) in xvals:
+                for opt in self.options:
+                    if opt.specify(k, v): break
+                else:
+                    print >>sys.stderr, "Error: Unknown option:", k
+                    self.usage()
+
+            # then process the 1st arg 
+            (arg,args) = (args[0], args[1:])
+
             isvar = 0
             if '=' in arg:
                 (k, v) = arg.split('=', 1)
@@ -312,8 +323,11 @@ class Opts:
                     if opt.specify(k, v):
                         isvar = 1
                         break
+            elif self.getdefault(arg):
+                isvar = 1
             if not isvar:
                 xargs.append(arg)
+
         return xargs
 
     def short_opts(self):